package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApLikesBehaviorMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.common.constants.message.HotArticleConstants;
import com.heima.model.Utils.threadlocal.AppThreadLocalUtils;
import com.heima.model.behavior.dto.LikesBehaviorDto;
import com.heima.model.behavior.pojo.ApBehaviorEntry;
import com.heima.model.behavior.pojo.ApLikesBehavior;
import com.heima.model.behavior.pojo.UpdateArticleMess;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojo.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by ZYJ on 2021/6/4 22:48
 */
@Service
@Slf4j
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper, ApLikesBehavior>implements ApLikesBehaviorService {
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    private KafkaTemplate kafkaTemplate;
    /**
     * 喜欢行为
     * @param dto
     * @return
     */
    @Override
    public ResponseResult like(LikesBehaviorDto dto) {
        ApUser user = AppThreadLocalUtils.getUser();
        ApBehaviorEntry apBehaviorEntry =
                apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        if (apBehaviorEntry == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        if(dto.getOperation().intValue() == 0){
            //点赞
            ApLikesBehavior likeBehavior = getOne(Wrappers.<ApLikesBehavior>lambdaQuery()
                    .eq(ApLikesBehavior::getArticleId, dto.getArticleId())
                    .eq(ApLikesBehavior::getEntryId, apBehaviorEntry.getId())
            );


            if(likeBehavior != null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"请勿重复点赞");
            }
            likeBehavior = new ApLikesBehavior();
            likeBehavior.setEntryId(apBehaviorEntry.getId());
            likeBehavior.setArticleId(dto.getArticleId());
            likeBehavior.setType((short)0);
            likeBehavior.setOperation((short)0);
            likeBehavior.setCreatedTime(new Date());
            this.save(likeBehavior);

        }else {
            //取消条件
            this.remove(Wrappers.<ApLikesBehavior>lambdaQuery()
                    .eq(ApLikesBehavior::getArticleId,dto.getArticleId())
                    .eq(ApLikesBehavior::getEntryId,apBehaviorEntry.getId())
            );
        }
        //计算热点文章 消息封装
        UpdateArticleMess updateArticleMess = new UpdateArticleMess();
        updateArticleMess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
        updateArticleMess.setArticleId(dto.getArticleId());
        updateArticleMess.setAdd(dto.getOperation() == 0 ? 1:-1);

        kafkaTemplate.send(HotArticleConstants.HOTARTICLE_SCORE_INPUT_TOPIC, JSON.toJSONString(updateArticleMess));
        log.info("点赞行为 热点文章计算消息已发送 消息内容 ==>",updateArticleMess);
        return ResponseResult.okResult();
    }

}
